﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Configuration;

namespace crawl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(txtURL.Text);

        }
        private void getText()
        {

            HtmlDocument doc = webBrowser1.Document;
            
            HtmlElementCollection hecTeamInfo = doc.GetElementsByTagName("a");
            foreach (HtmlElement el in hecTeamInfo)
            {
                string link = el.GetAttribute("href");

                if (link.Length < (24 + 17)) continue;

                if (link.StartsWith("http://media.daum.net/v/"))
                {
                    string strLinkInfo = link.Substring(24,17) + "|" 
                        + el.GetAttribute("href") + "|" + el.InnerText;

                    if (lstArticles.Items.Contains(strLinkInfo)) continue;

                    lstArticles.Items.Add(strLinkInfo);
                }
                /*
                if (el.GetAttribute("className") == "tbl main_season_kbo")
                {
                    textBox2.Text += el.InnerText;
                }
                 * */
            }

            MessageBox.Show("완료");


        }

        private void button2_Click(object sender, EventArgs e)
        {
            getText();

        }

        private void lstArticles_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            webBrowser1.Navigate(lstArticles.SelectedItem.ToString());
        }


        private void lstArticles_SelectedIndexChanged(object sender, EventArgs e)
        {

            string[] aa = lstArticles.SelectedItem.ToString().Split('|');
            txtOut.Text = "링크 : " + aa[0] + Environment.NewLine + "제목 : " + aa[1];
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings;
            string conString = connections["MyTemplate"].ConnectionString;

            //string conString = System.Configuration.ConfigurationManager.AppSettings["MyTemplate"];
            SqlConnection connection = new SqlConnection(conString);


            connection.Open();

            foreach (object item in lstArticles.Items)
            {
                string[] aa = item.ToString().Split('|');
                insertLinkTitle(connection, aa[0], aa[1], aa[2]);
            }
            connection.Close();
            MessageBox.Show("저장 완료");
        }
        private void insertLinkTitle(SqlConnection connection, string articleNo, string link, string title)
        {
            SqlCommand insertCommand = new SqlCommand("spInsertLinkTitle", connection);
            insertCommand.CommandType = CommandType.StoredProcedure;
            insertCommand.Parameters.Add(new SqlParameter("@ArticleNo", articleNo));
            insertCommand.Parameters.Add(new SqlParameter("@Link", link));
            insertCommand.Parameters.Add(new SqlParameter("@Title", title));
            insertCommand.ExecuteNonQuery();
        }

        private void btnDBSelect_Click(object sender, EventArgs e)
        {
            ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings;
            string conString = connections["MyTemplate"].ConnectionString;

            SqlConnection connection = new SqlConnection(conString);

            connection.Open();

            string sql = "Select * from tbArticles";
            SqlCommand selectCommand = new SqlCommand(sql,connection);

            SqlDataReader sdr = selectCommand.ExecuteReader();
            while (sdr.Read())
            {
                string strLinkInfo = sdr["ArticleNo"] + "|" + sdr["Link"] + "|" + sdr["Title"];

                if (lstArticles.Items.Contains(strLinkInfo)) continue;

                lstArticles.Items.Add(strLinkInfo);
            }

            connection.Close();

        }   

    }
}
